package com.tc.travel.submitter.service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tc.travel.submitter.pojo.VisaAddUpdateDto;
import com.tc.travel.submitter.pojo.VisaCancelDto;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.Json;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.StringUtil;
import com.tc.travel.util.exception.ComRuntimeException;

@Component
public class VisaBo {
	private static Logger log = Logger.getLogger(VisaBo.class);

	// 获取要提交的visa
	public VisaAddUpdateDto getVisaForAddUpdate() {
		String url = PropertyUtil.interfaceUrlVisaGetForAddUpdate;
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doGet(url, params, headers);

		if (StringUtil.isEmpty(jsonStr)) {
			return null;
		}

		Json<VisaAddUpdateDto> json = toJsonVisaAddUpdateDto(jsonStr);

		if (json.isSuccess()) {
			return json.getObj();
		} else {
			throw new ComRuntimeException("QueryForSubmit error: " + json.getMsg());
		}
	}

	public static Json<VisaAddUpdateDto> toJsonVisaAddUpdateDto(String jsonStr) {
		Type jsonType = new TypeToken<Json<VisaAddUpdateDto>>() {
		}.getType();
		Json<VisaAddUpdateDto> json = new Gson().fromJson(jsonStr, jsonType);
		return json;
	}

	// 获取要撤消的visa
	public VisaCancelDto getVisaForCancel() {
		String url = PropertyUtil.interfaceUrlVisaGetForCancel;
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doGet(url, params, headers);

		if (StringUtil.isEmpty(jsonStr)) {
			return null;
		}

		Json<VisaCancelDto> json = toJsonVisaCancelDto(jsonStr);

		if (json.isSuccess()) {
			return json.getObj();
		} else {
			throw new ComRuntimeException("queryForCancel error: " + json.getMsg());
		}
	}

	public static Json<VisaCancelDto> toJsonVisaCancelDto(String jsonStr) {
		Type jsonType = new TypeToken<Json<VisaCancelDto>>() {
		}.getType();
		Json<VisaCancelDto> json = new Gson().fromJson(jsonStr, jsonType);
		return json;
	}

	// 更新状态
	// visaCancelId非空，则更新visa_cancel表的状态
	public void updateVisaState(Long visaInfoId, Long visaCancelId, int state, String destVisaId, String errorMsg) {
		String url = PropertyUtil.interfaceUrlVisaUpdateState;
		Map<String, String> params = new HashMap<String, String>();
		params.put("visaInfoId", visaInfoId + "");

		// visaCancelId非空，则更新visa_cancel表的状态
		if (visaCancelId != null && visaCancelId > 0) {
			params.put("visaCancelId", visaCancelId + "");
		}

		params.put("state", state + "");
		params.put("destVisaId", destVisaId);
		params.put("errorMsg", errorMsg);

		Map<String, String> headers = new HashMap<String, String>();

		String jsonStr = HttpClientUtil.doPost(url, params, headers);
		log.info(jsonStr);
	}
}
